var fsystem
//创建FileViewer，用于浏览文件
function CreateFileViewer(){
	//子类的构造函数
	//创建FileInfo，用于显示文件信息
	function CreateFileInfo(){
		var name=document.createElement("a")
		var size=document.createElement("em")
		var permission=document.createElement("em")
		var type=document.createElement("em")
		fileInfo=document.createElement("div")
		fileInfo.load=function(data){
			name.textContent=data.Name
			name.href=fsystem.getDownloadPath(data.Path)
			size.textContent=data.Size
			permission.textContent=data.Permission
			type.textContent=data.MimeType
			$(fileInfo).toggleClass("have-info",true)
			if(data.MimeType.match("image/")){
					var path=fsystem.getDownloadPath(data.Path)
					var list=viewer.createImageList()
					var index=0
					for( var i in list){
						if(list[i]==path){
							index=i
							break
						}
					}
					imageViewer=CreateSimpleImageViewer(list,index)
					viewer.popWindow.setElement(imageViewer)
			}
		}
		fileInfo.clean=function(){
			name.textContent=""
			size.textContent=""
			permission.textContent=""
			$(fileInfo).toggleClass("have-info",false)
			type.textContent=""
		}
		$(fileInfo).addClass("file-info")
		$(fileInfo).append("<div class='name-wapper'><h3>Name:</h3></div>")
		$(fileInfo).children('.name-wapper').append(name)
		$(fileInfo).append("<div class='size-wapper'><h3>Size:</h3></div>")
		$(fileInfo).children('.size-wapper').append(size)
		$(fileInfo).append("<div class='permission-wapper'><h3>Permission:</h3></div>")
		$(fileInfo).children('.permission-wapper').append(permission)
		$(fileInfo).append("<div class='type-wapper'><h3>Type:</h3></div>")
		$(fileInfo).children('.type-wapper').append(type)
		$(name).addClass("name")
		$(size).addClass("size")
		$(type).addClass("type")
		$(permission).addClass("permission")
		return fileInfo
	}
	//创建ErrorBox，用于显示系统错误
	function CreateErrorBox(){
		errBox=document.createElement("div")
		errBox.setError=function(errString){
			errBox.textContent=errString
			$(errBox).toggleClass("have-err",true)
		}
		errBox.clean=function(){
			errBox.textContent=""
			$(errBox).toggleClass("have-err",false)
		}
		$(errBox).addClass("err-box")
		return errBox
	}
	//创建IconViewer，用于显示图标
	function CreateIconViewer(){
		var  view=document.createElement("div")
		function CreateIcon(file){
			var  icon=document.createElement("div")
			var  img=CreateImageElement()
			var  name=document.createElement("p")
			var err=document.createElement("div")
			icon.load=function(info){
				if(info.Error){
					err.textContent=info.Error
				}
				if(info.IsFolder){
					this.isFolder()
				}else{
					this.notFolder(info)
				}
				icon.title=info.Name
			}
			icon.isFolder=function(){
				img.src=IMAGE_FOLDER
			}
			icon.notFolder=function(info){
				if(info.MimeType.match("image/")){
					img.src=fsystem.getThumbnailPath(info.Path)
				}else{
					img.src=IMAGE_FILE
				}
			}
			icon.name=file.Name
			name.textContent=file.Name
			$(icon).append("<div class='wapper'></div>")
			$(icon).children(".wapper").append(img)
			$(icon).append(name)
			$(icon).append(err)
			$(icon).addClass("icon")
			$(name).addClass("name")
			$(err).addClass("err")
			icon.load(file)

			$(icon).click(function(){
				view.changeFolderHandler(file.Path)
			})
			return icon
		}
		view.load=function(data){
			view.empty()
			if(data.Children.length==0){
				$(view.container).append("<em>No files in this folder</em>")
				return
			}
			//只添加目录，略过文件
			for(i in data.Children){
				if(!data.Children[i].IsFolder)continue
				view.appendIcon(data.Children[i])
			}
			//只添加文件，略过目录
			for(i in data.Children){
				if(data.Children[i].IsFolder)continue
				view.appendIcon(data.Children[i])
			}
			view.show()
		}
		view.setChangeFolderHandler=function(handler){
			view.changeFolderHandler=handler
		}
		view.appendIcon=function(icon){
			len=view.icons.length
			view.icons[len]=icon
		}
		view.show=function(){
			var on=false
			append=function(){
				if(on==true)return
				on=true
				for(var i=0;i<100;i++){
					if(view.currentIcon>=view.icons.length)break
					if(!(i%50)){
						spliter=CreateSpliter("h")
						$(view.container).append(spliter)
						spliter.textContent=((view.currentIcon==0)?"Totle:":(view.currentIcon+"/"))+view.icons.length
					}
					var icon=CreateIcon(view.icons[view.currentIcon++])
					$(view.container).append(icon)
				}
				$(view.container).ready(function(){on=false})
			}
			append()
			$(window).scroll(function (){
				if(pageYOffset+innerHeight+200>=$(document).height()){
					append()
				}
			})
		}
		view.empty=function(){
			var oldContainer=$(view).children(".container")
			if(oldContainer){
				oldContainer.addClass("destory")
				setTimeout(function(){oldContainer.detach()},500)
			} 	
			view.container=document.createElement("div")
			$(view.container).addClass("container")
			$(view).append(view.container)
			$(view.container).addClass("show")
			setTimeout(function(){
				$(view.container).toggleClass('show',false)
			},500)
			view.icons=Array()
			view.currentIcon=0
		}
		$(view).addClass("icon-view")
		return view
	}
	//创建导航栏，用于显示路径
	function CreateNavigation(){
		var nav=document.createElement("div")
		var container=document.createElement("div")
		function CreateNavigationCell(path){
			var  cell=document.createElement("button")
			$(cell).addClass("navigation-cell")
			cell.setText=function(text){
				this.textContent=text
				$(cell).attr("value",names[i])
			}
			$(cell).click(function(){
				nav.changeFolderHandler(path)
			})
			return cell
		}
		function CreateNavigationDriveList(driveList){
			var list=document.createElement("ul")
			$(list).addClass("drive-list")
			drives=driveList.split(";")
			$(list).append("<p class='navigation-cell'>Drives</p>")
			for(i in drives){
				var li=document.createElement("li")
				li.textContent=drives[i]
				li.path=drives[i]
				$(li).click(function(){
					nav.changeFolderHandler(this.path)
				})
				$(li).addClass("navigation-cell")
				$(li).attr("value",li.path.match(/[A-Za-z]:/g))
				$(list).append(li)
			}
			return list
		}
		nav.load=function(data){
			if(nav.currentFolder.indexOf(data.Path)==-1){
				nav.changeFolder(data.Path)
			}
			nav.highlightFolder(data.Path)
		}
		nav.changeFolder=function(path){
			container.empty()
			names=path.split(fsystem.PathSeparator)
			if(names[0]=="")names[0]="/"
			var path=""
			for (i in names){
				if(names[i]=="")continue
				path+=names[i]+fsystem.PathSeparator
				cell=CreateNavigationCell(path)
				cell.setText(names[i])
				$(container).append(cell)
			}
			if(fsystem.OS=="windows"){
				var cell=CreateNavigationDriveList(fsystem.DriveList)
				$(container).append(cell)
			}
			nav.currentFolder=path
		}
		nav.highlightFolder=function(url){
			$(container).children().toggleClass("highlight",false)
			names=url.split(fsystem.PathSeparator)
			for(var i=1;i<=names.length;i++){
				name=names[names.length-i]
				if(name!="")break
			}
			if(fsystem.OS=="windows"){
				$(container).children("drive-list").children("navigation-cell").filter('[value="'+names[0]+'"]').toggleClass("highlight",true)
			}
			$(container).children().filter('[value="'+name+'"]').toggleClass("highlight",true)
		}
		nav.setChangeFolderHandler=function(handler){
			nav.changeFolderHandler=handler
		}
		container.empty=function(){
			$(this).empty()
		}
		nav.currentFolder=""
		$(nav).addClass("navigation")
		$(container).addClass("container")
		$(nav).append(container)
		return nav
	}
	//声明变量
	var  viewer=new Object()
	var  nav=CreateNavigation()
	var  iconViewer=CreateIconViewer()
	var  fileInfo=CreateFileInfo()
	var errBox=CreateErrorBox()
	viewer.load=function(data){
		errBox.clean()
		fileInfo.clean()
		if(data.Error){
			errBox.setError(data.Error)
		}
		else if(data.IsFolder){
			viewer.currentFolderData=data
			nav.load(data)
			iconViewer.load(data)
		}else{
			fileInfo.load(data)
		}
	}
	viewer.loadPath=function(path){
		fsystem.getFileInfo(path,function(data){
			viewer.load(data[0])
		})
	}
	viewer.getAppName=function(){
		return "File Viewer"
	}
	viewer.getAppNameS=function(){
		return "file-viewer"
	}
	viewer.getControlElement=function(){
		var container=document.createElement("div")
		
		$(container).append(fileInfo)
		return container
	}
	viewer.getMainContain=function(){
		var a=document.createElement("div")
		$(a).append(nav)
		$(a).append(iconViewer)
		return a
	}
	viewer.createImageList=function(){
		if(viewer.imageList&&viewer.imageList.path==viewer.currentFolderData.Path){
			return viewer.imageList
		}
		var files= viewer.currentFolderData.Children
		var list=Array()
		for(var i in files){
			var file=files[i]
			if(file.MimeType.match("image/")){
				list[list.length]=fsystem.getDownloadPath(file.Path)
			}
		}
		list.path=viewer.currentFolderData.Path
		viewer.imageList=list
		return list
	}
	nav.setChangeFolderHandler(viewer.loadPath)
	iconViewer.setChangeFolderHandler(viewer.loadPath)
	fsystem.getFileInfo(fsystem.HomeFolder,function(data){
		viewer.load(data[0])
	})
	return viewer
}

